<script setup lang="ts">
import { mobileRules, passwordRules } from '@/utils/rules'
import { showToast } from 'vant'
import { loginAPI } from '@/services/user'
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const store = useUserStore()
// 密码登录数据结构
const isAgree = ref(false)
const mobile = ref('13230000002')
const password = ref('abc12345')

// 表单提交函数
const submitForm = async () => {
  if (!isAgree.value) {
    showToast('请同意用户协议')
    return
  }

  console.log('这里是提交表单')
  // 发起登录请求
  const res = await loginAPI(mobile.value, password.value)
  // 将数据存入仓库
  store.setUser(res.data)

  // 尝试从 路由中获取 redirect 参数如果有就跳转
  // 没有就跳到登录页
  if (route.query.redirect) {
    router.push(route.query.redirect as string) // 跳转到指定的页面
  } else {
    router.push('/') // 跳转到首页
  }
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册"></cp-nav-bar>
    <div class="login-head">
      {{ store.user.mobile }}
      <h3>密码登录</h3>
      <a href="javascript:;">
        <span>短信验证码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form 表单 -->
    <!-- 表单的提交事件, 一旦触发, 就会默认进行整个表单的校验, -->
    <!-- 如果没问题, 就会触发回调函数 -->
    <van-form autocomplete="off" @submit="submitForm">
      <van-field
        v-model="mobile"
        :rules="mobileRules"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        v-model="password"
        :rules="passwordRules"
        placeholder="请输入密码"
        type="password"
      ></van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <!-- van-button 的 native-type 是 submit 的话
        每当点击, 所在的表单就会触发提交事件 -->
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
</style>
